home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / pdevtest / client.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-01  |  5.1 KB  |  243 lines

  1. /* 
  2.  * client.c --
  3.  *
  4.  *    The client part of some multi-program synchronization primatives.
  5.  *    The routines here interface to the server; initial contact,
  6.  *    waiting for the start message, and notification of completion.
  7.  *
  8.  * Copyright 1986 Regents of the University of California
  9.  * All rights reserved.
  10.  */
  11.  
  12. #ifndef lint
  13. static char rcsid[] = "$Header: /sprite/src/tests/pdevtest/RCS/client.c,v 1.1 88/04/17 10:20:46 brent Exp Locker: brent $ SPRITE (Berkeley)";
  14. #endif not lint
  15.  
  16.  
  17. #include <sprite.h>
  18. #include <status.h>
  19. #include <stdio.h>
  20. #include <fs.h>
  21. #include <sys/file.h>
  22. #include <dev/pdev.h>
  23. #include "pdevInt.h"
  24.  
  25. extern char *pdev;
  26.  
  27. typedef struct ClientState {
  28.     int clientStreamID;
  29. } ClientState;
  30.  
  31. /*
  32.  *----------------------------------------------------------------------
  33.  *
  34.  * ClientSetup --
  35.  *
  36.  *    Establish contact with the server.
  37.  *
  38.  * Results:
  39.  *    A pointer to state about the clients needed by ClientStart and
  40.  *    ClientDone.
  41.  *
  42.  * Side effects:
  43.  *    Creates named pipes and communicates with server
  44.  *    This exits upon error.
  45.  *
  46.  *----------------------------------------------------------------------
  47.  */
  48.  
  49. void
  50. ClientSetup(dataPtr)
  51.     ClientData *dataPtr;
  52. {
  53.     ClientState *statePtr;
  54.     ReturnStatus status;
  55.  
  56.     statePtr = (ClientState *)malloc(sizeof(ClientState));
  57.  
  58.     statePtr->clientStreamID = open(pdev, O_RDWR);
  59.     if (statePtr->clientStreamID < 0) {
  60.     perror("ClientSetup: error opening pseudo device");
  61.     exit(status);
  62.     }
  63.     *dataPtr = (ClientData)statePtr;
  64. }
  65.  
  66. /*
  67.  *----------------------------------------------------------------------
  68.  *
  69.  * ClientRead --
  70.  *
  71.  *    Read from a pseudo-device.  The amount and number of repetitions
  72.  *    can be varied for measurment.
  73.  *
  74.  * Results:
  75.  *    None
  76.  *
  77.  * Side effects:
  78.  *    None.
  79.  *
  80.  *----------------------------------------------------------------------
  81.  */
  82.  
  83. void
  84. ClientRead(data, size, reps)
  85.     ClientData data;
  86.     int size;
  87.     register int reps;
  88. {
  89.     ClientState *statePtr;
  90.     int amountRead;
  91.     register ReturnStatus status;
  92.     char buffer[MAX_SIZE];
  93.     register int i;
  94.  
  95.     statePtr = (ClientState *)data;
  96.     if (size > MAX_SIZE) {
  97.     size = MAX_SIZE;
  98.     }
  99.     for (i=0 ; i<reps ; i++) {
  100.     amountRead = size;
  101.     if (selectP) {
  102.         int numReady;
  103.         int mask = 1 << statePtr->clientStreamID;
  104.         status = Fs_Select(32, NULL, &mask, NULL, NULL, &numReady);
  105.         if (status != SUCCESS) {
  106.         Stat_PrintMsg(status, "ClientRead: error on select");
  107.         break;
  108.         }
  109.     }
  110.     amountRead = read(statePtr->clientStreamID, buffer, size);
  111.     if (amountRead < 0) {
  112.         perror("ClientRead: error on read");
  113.         break;
  114.     } else if (amountRead != size) {
  115.         fprintf(stderr, "Read #%d was short (%d < %d)\n",
  116.             i, amountRead, size);
  117.         break;
  118.     }
  119.     if (size > 0 && buffer[0] != 'z') {
  120.         fprintf(stderr, "Bad data returned <%c>\n", buffer[0]);
  121.     }
  122.     }
  123. }
  124.  
  125. /*
  126.  *----------------------------------------------------------------------
  127.  *
  128.  * ClientWrite --
  129.  *
  130.  *    Write from a pseudo-device.  The amount and number of repetitions
  131.  *    can be varied for measurment.
  132.  *
  133.  * Results:
  134.  *    None
  135.  *
  136.  * Side effects:
  137.  *    None.
  138.  *
  139.  *----------------------------------------------------------------------
  140.  */
  141.  
  142. void
  143. ClientWrite(data, size, reps)
  144.     ClientData data;
  145.     int size;
  146.     register int reps;
  147. {
  148.     ClientState *statePtr;
  149.     int amountWrite;
  150.     register ReturnStatus status;
  151.     char buffer[MAX_SIZE];
  152.  
  153.     statePtr = (ClientState *)data;
  154.     if (size > MAX_SIZE) {
  155.     size = MAX_SIZE;
  156.     }
  157.     do {
  158.     amountWrite = write(statePtr->clientStreamID, buffer, size);
  159.     if (amountWrite < 0) {
  160.         perror("ClientWrite: error on write");
  161.         break;
  162.     } if (amountWrite != size) {
  163.         fprintf(stderr, "Short write %d < %d\n", amountWrite,
  164.                     size);
  165.     }
  166.     } while (--reps > 0);
  167. }
  168.  
  169. /*
  170.  *----------------------------------------------------------------------
  171.  *
  172.  * ClientIOControl --
  173.  *
  174.  *    Do an IOControl to a pseudo-device.
  175.  *    The amount of data passed in and number of repetitions
  176.  *    can be varied for measurment.
  177.  *
  178.  * Results:
  179.  *    None
  180.  *
  181.  * Side effects:
  182.  *    None.
  183.  *
  184.  *----------------------------------------------------------------------
  185.  */
  186.  
  187. void
  188. ClientIOControl(data, size, reps)
  189.     ClientData data;
  190.     int size;
  191.     register int reps;
  192. {
  193.     ClientState *statePtr;
  194.     int amountRead;
  195.     register ReturnStatus status;
  196.     int command;
  197.     char inBuffer[MAX_SIZE];
  198.     char outBuffer[MAX_SIZE];
  199.  
  200.     statePtr = (ClientState *)data;
  201.     do {
  202.     extern Boolean switchBuf;
  203.     if (switchBuf && (reps % 7) == 0) {
  204.         command = IOC_PDEV_SET_BUF;
  205.     } else {
  206.         command = 8 << 16;    /* To avoid range of generic I/O controls */
  207.     }
  208.     status = Fs_IOControl(statePtr->clientStreamID, command, size, inBuffer,
  209.                 size, outBuffer);
  210.     if (status != SUCCESS) {
  211.         Stat_PrintMsg(status, "ClientIOControl: error ");
  212.         break;
  213.     }    
  214.     } while (--reps > 0);
  215. }
  216.  
  217. /*
  218.  *----------------------------------------------------------------------
  219.  *
  220.  * ClientDone --
  221.  *
  222.  *    Tell the server we're done.  This is just done by closing
  223.  *    the pseudo stream.
  224.  *
  225.  * Results:
  226.  *    None
  227.  *
  228.  * Side effects:
  229.  *    None
  230.  *
  231.  *----------------------------------------------------------------------
  232.  */
  233.  
  234. void
  235. ClientDone(data)
  236.     ClientData data;
  237. {
  238.     ClientState *statePtr;
  239.  
  240.     statePtr = (ClientState *)data;
  241.     close(statePtr->clientStreamID);
  242. }
  243.